<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :footer="null"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="申请人">
          <a-input disabled v-decorator="['createName', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="所属部门">
          <j-dict-select-tag :triggerChange="true" disabled dictCode="sys_depart,depart_name,org_code"
                             v-decorator="[ 'sysOrgCode', {}]">
          </j-dict-select-tag>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="单证类型">
          <j-dict-select-tag :triggerChange="true" disabled dictCode="document"
                             v-decorator="[ 'type', validatorRules.type ]">
          </j-dict-select-tag>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="盖章说明">
          <a-textarea v-decorator="[ 'reason', validatorRules.reason ]" :autoSize="{ minRows: 2, maxRows: 4 }"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否加急">
          <j-dict-select-tag :triggerChange="true" dictCode="status_en"
                             v-decorator="[ 'dispatch', validatorRules.dispatch ]">
          </j-dict-select-tag>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="申请时间">
          <a-date-picker style="width: 100%" showTime disabled format='YYYY-MM-DD HH:mm:ss'
                         v-decorator="[ 'createTime', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="审批状态">
          <j-dict-select-tag :triggerChange="true" disabled dictCode="bpm_status"
                             v-decorator="[ 'appstatus', validatorRules.appstatus ]">
          </j-dict-select-tag>
        </a-form-item>
      </a-form>

      <a-button style="margin-left: 80%" type="primary" @click="handleOk">提交</a-button>
    </a-spin>

    <local-file @delCheckFun="delCheckFun" ref="localFile"></local-file>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from 'moment'
  import { mapActions, mapGetters } from 'vuex'
  import LocalFile from '../../modules/oss/LocalFile'


  export default {
    name: 'ActeChapterModal',
    components: { LocalFile },
    data() {
      return {
        title: '操作',
        visible: false,
        model: {},
        fk: '',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          type: {
            rules: [{ required: true, message: '请输入单证类型!' }],
            initialValue: '101'
          },
          reason: {
            rules: [{ required: true, message: '请输入申请说明!' }]
          },
          dispatch: {
            rules: [{ required: true, message: '请输入是否加急!' }],
            initialValue: 'no'
          },
          appstatus: { initialValue: '1' }
        },
        url: {
          add: '/acteChapter/add',
          edit: '/acteChapter/edit'
        }
      }
    },
    created() {
    },
    methods: {
      moment,
      //获取当前登录人信息
      ...mapGetters(['userInfo']),

      add() {
        this.edit({
          createName: this.userInfo().realname,
          sysOrgCode: this.userInfo().orgCode
        })
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'createName', 'sysOrgCode', 'type', 'reason', 'dispatch', 'appstatus'))
          //时间格式化
          this.form.setFieldsValue({ createTime: this.model.createTime ? moment(this.model.createTime) : null })
          this.$refs.localFile.fk=this.model.id
          this.$refs.localFile.sd(this.model.id,true)
        })

      },
      close() {
        this.$emit('close')
        this.visible = false
        this.$refs.localFile.fk=''
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            //时间格式化
            formData.createTime = formData.createTime ? formData.createTime.format('YYYY-MM-DD HH:mm:ss') : null

            console.log(formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                this.model.id = res.result.id
                this.$refs.localFile.fk=this.model.id
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              // that.close();
            })


          }
        })
      },
      handleCancel() {
        this.close()
      },
      delCheckFun(callback) {
        let result = this.model.id
        //业务逻辑代码...
        callback(result) // 执行作为参数的函数，注意：callback(true)写法，要赋值给变量result
      }

    }
  }
</script>

<style lang="less" scoped>

</style>